package com.fin.zw.aiqas.controller;

import com.fin.zw.aiqas.annotation.CurrentUser;
import com.fin.zw.aiqas.entity.InspectionInfo;
import com.fin.zw.aiqas.model.request.InspectionFileRequest;
import com.fin.zw.aiqas.model.request.InspectionInfoRequest;
import com.fin.zw.aiqas.model.session.SessionModel;
import com.fin.zw.aiqas.service.InspectionService;
import com.fin.zw.aiqas.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;


@RestController
@RequestMapping("/inspection")
@Api(value = "质检结果", description = "质检结果")
public class InspectionController {

    @Autowired
    InspectionService service;
    private final static Logger log = LoggerFactory.getLogger(InspectionController.class);

    @RequestMapping(value = "/update", method = {RequestMethod.PUT, RequestMethod.PATCH})
    @ApiOperation(value = "修改质检结果", notes = "修改质检结果")
    public ResponseEntity<Void> putInspection(@RequestBody InspectionInfoRequest request,@ApiIgnore @CurrentUser SessionModel session) {
        try {
            service.updateInspection(request,session);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().build();
        }
    }


    /**
     * 查询质检规则列表
     */
    @GetMapping("/fetchPage")
    @ApiOperation(value = "查询质检规则列表信息", notes = "查询质检规则列表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小"),
            @ApiImplicitParam(name = "sort", dataType = "string", paramType = "query", value = "依据什么排序: 属性名(asc|desc).", allowMultiple = true)
    })
    public ResponseEntity<Page<InspectionInfo>> queryInspectionTable(InspectionInfoRequest request,Pageable pageable) {
        try {
            log.info("查询质检规则列表信息...");
            return ResponseEntity.ok().body(service.queryRuleTable(request, pageable));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().build();
        }
    }

    @ApiOperation(value = "导出质检结果信息", notes = "导出质检结果信息")
    @PostMapping("/exportQualityResult")
    public void queryInspectionTable(HttpServletResponse response,  InspectionFileRequest request) {
        try {
            log.info("文件导出开始........");
            File file = service.exportFile(request);
            FileUtil.download(response, file);
            file.delete();
            log.info("文件制作完成开始下载........");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "根据定时任务ID查询对应质检结果", notes = "根据定时任务ID查询对应质检结果")
    @GetMapping("/fetchByTaskId/{taskId}")
    public ResponseEntity<List<InspectionInfo>> fetchByTaskId(@PathVariable Long taskId) {
        List<InspectionInfo> inspectionInfos = service.fetchByTaskId(taskId);
        return ResponseEntity.ok().body(inspectionInfos);
    }
}
